package com.example.myapplication.logisticsinquiry;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.example.myapplication.ShuJu.LogisticsinquirynoBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ComplainActivity extends AppCompatActivity {

    private String COMPANYID;
    private ImageView img;
    private TextView companyname;
    private TextView infoNo;
    private EditText description;
    private TextView submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complain);
        initview();
        //返回
        but_bloak();
    }

    private void but_bloak() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ComplainActivity.this.finish();
            }
        });
    }

    private void initview() {
        Intent intent = getIntent();
        Gson gson = new Gson();
        COMPANYID = intent.getStringExtra(Commom.COMPANYID);
        LogisticsinquirynoBean logisticsinquirynoBean = gson.fromJson(COMPANYID, LogisticsinquirynoBean.class);


        img = findViewById(R.id.img);
        companyname = findViewById(R.id.companyname);
        infoNo = findViewById(R.id.infoNo);
        description = findViewById(R.id.description);
        submit = findViewById(R.id.submit);
        //填充信息
        fillinformation(logisticsinquirynoBean);
    }

    private void fillinformation(LogisticsinquirynoBean logisticsinquirynoBean) {
        LogisticsinquirynoBean.DataBean.CompanyBean company = logisticsinquirynoBean.getData().getCompany();
        companyname.setText(company.getName());
        infoNo.setText(logisticsinquirynoBean.getData().getInfoNo());


        //属快递公司 ID
        String id = String.valueOf(logisticsinquirynoBean.getData().getCompanyId());
        //快递单号
        String infoNo = logisticsinquirynoBean.getData().getInfoNo();
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String descriptionString = description.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("companyId", id);
                    jsonObject.put("infoNo", infoNo);
                    jsonObject.put("description", descriptionString);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //jsonObject.put("questionType",)
                String JSON = jsonObject.toString();
                sendJSONfillin(JSON);
            }
        });
    }

    private void sendJSONfillin(String json) {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_complaint",
                OKHttpTool.POST,
                LeavecommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LeavecommentBean leavecommentBean = (LeavecommentBean) obj;
                                if (leavecommentBean.getCode() == 200) {
                                    Toast.makeText(ComplainActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }, json);
    }
}